//
//  XingAndLabel_UIView.m
//  MTSIPAD
//
//  Created by 3g2win 3g2win on 12-9-12.
//  Copyright (c) 2012年 3g2win. All rights reserved.
//

#import "XingAndLabel_UIView.h"
#import "ApplicationUtils.h"
#import "ISMutileButton.h"
@implementation XingAndLabel_UIView
@synthesize titleLabel;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//初始化函数
- (id)initWithFrame:(CGRect)frame isNecessary:(BOOL)isNeedB 
       withTitleStr:(NSString*)titleStr with_FontOfSize:(float)fontSizef{
    //super method
    self = [super initWithFrame:frame];
    if (self) {
        float x,y,width,height;
        x=15;  y=(frame.size.height-30)/2; width=30; height=30;
        ISMutileButton* xinghao_btn=[[ISMutileButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [xinghao_btn setBackgroundColor:[UIColor clearColor]];
        xinghao_btn.isMultile = isNeedB;
        //选择视图 30*30
        if (isNeedB) {
            [xinghao_btn setBackgroundImage:[ApplicationUtils getLocalImage:@"st_Options_mark_normal@2x"] forState:UIControlStateNormal];
            [xinghao_btn setBackgroundImage:[ApplicationUtils getLocalImage:@"st_Options_mark_hover@2x"] forState:UIControlStateSelected];
        }else {
            [xinghao_btn setBackgroundImage:[ApplicationUtils getLocalImage:@"st_Options_mark_d_normal"] forState:UIControlStateNormal];
            [xinghao_btn setBackgroundImage:[ApplicationUtils getLocalImage:@"st_Options_mark_d_hover"] forState:UIControlStateSelected];
        }
        [xinghao_btn setTag:88];
        [self addSubview:xinghao_btn];
        //设置Label属性
        x=x+width+3; y=2; height=frame.size.height;
//        UIFont* titleFont=[UIFont systemFontOfSize:fontSizef];
//        width=[titleStr sizeWithFont:titleFont].width;
        width = frame.size.width-20;
//        titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
//        [titleLabel setBackgroundColor:[UIColor clearColor]];
//        [titleLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
//        [titleLabel setTextAlignment:UITextAlignmentLeft];
//        [titleLabel setFont:titleFont];
//        [titleLabel setTextColor:[UIColor blackColor]];
//        [titleLabel setText:titleStr];
//        [self addSubview:titleLabel];
        
        CGSize size = CGSizeMake(width, 1000);
        CGSize font_Size = [titleStr sizeWithFont:[UIFont systemFontOfSize:fontSizef] constrainedToSize:size];
        titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(x, y, width, font_Size.height)];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setFont:[UIFont systemFontOfSize:fontSizef]];
        [titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
        [titleLabel setNumberOfLines:0];
//        [titleLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
        [titleLabel setTextAlignment:NSTextAlignmentLeft];
        [titleLabel setText:titleStr];
        
        
        UIWebView *selectionView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,width-5,height)];
        [selectionView setBackgroundColor:[UIColor clearColor]];
        [selectionView setOpaque:NO];
        [selectionView setTag:10];
        [selectionView setDelegate:self];
        NSString *HTMLData = titleStr;
        NSString *path = [FMPAPERSAVE_PATH stringByAppendingPathComponent:@"image"];
        [selectionView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:path]];
        [self addSubview:selectionView];
        
        //属性设置
        [self setBackgroundColor:[UIColor clearColor]];
        //自适应
//        width=x+width;
        width = frame.size.width-10;
        x=frame.origin.x;  y=frame.origin.y; height=frame.size.height;
        [self setFrame:CGRectMake(x, y, width, height)];
        
        ISMutileButton *choseBtn = [ISMutileButton buttonWithType:UIButtonTypeCustom];
        [choseBtn setFrame:CGRectMake(x+45, 0, frame.size.width-40, height)];
        [choseBtn setBackgroundColor:[UIColor clearColor]];
        [choseBtn setBackgroundImage:[ApplicationUtils getLocalImage:@"selector_lib_item_bg"] forState:UIControlStateHighlighted];
        choseBtn.isMultile = isNeedB;
        [choseBtn setTag:89];
        [self addSubview:choseBtn];
    }
    return self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // finished loading, hide the activity indicator in the status bar
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
    UIWebView *webView_ = (UIWebView *)[self viewWithTag:10];
    if (webView_) {
        [webView_ setFrame:CGRectMake(webView_.frame.origin.x, webView_.frame.origin.y, webView_.frame.size.width, webView_.scrollView.contentSize.height)];
    }
    ISMutileButton *isMutileButton = (ISMutileButton *)[self viewWithTag:89];
    if (isMutileButton) {
        [isMutileButton setFrame:CGRectMake(webView_.frame.origin.x, webView_.frame.origin.y, webView_.frame.size.width, webView_.scrollView.contentSize.height)];
    }
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, webView_.frame.size.width, webView_.scrollView.contentSize.height)];
    if (delegate!=nil&&[delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
        [delegate webViewDidFinishLoad:webView];
    }
}
- (void)dealloc {
    FMSCHBLog(@"<<-->Xing_Label_UIView-->dealloc-->释放内存");
    delegate = nil;
    if (titleLabel) {
        self.titleLabel=nil;
    }
     [[NSURLCache  sharedURLCache ] removeAllCachedResponses];
}


@end
